package com.mmall.controller.admin;

import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.ProductService;
import com.mmall.service.UserService;
import com.mmall.util.PropertiesUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/product/manager")
public class ProductManagerController {
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    //修改或保存商品
    @RequestMapping("saveOrUpdate.do")
    @ResponseBody
     public ServerResponse saveOrUpdate(Product product,HttpSession session){
         User user = (User) session.getAttribute(Const.CURRENT_USER);
         if(user==null){
             return ServerResponse.createServerResponseByFailure("请先登录");
         }
         if (userService.checkAdminRole(user).checkIsSuccess()) {
             return productService.saveOrUpdate(product);
         }
         else{
             return ServerResponse.createServerResponseByFailure("无权操作");
         }
     }
     //修改商品状态
     @RequestMapping("updatestatus.do")
     @ResponseBody
     public ServerResponse updateStatus(HttpSession session,Integer status,Integer categoryId){
         User user = (User) session.getAttribute(Const.CURRENT_USER);
         if(user==null){
             return ServerResponse.createServerResponseByFailure("请先登录");
         }
         if (userService.checkAdminRole(user).checkIsSuccess()) {
             return productService.updateStatus(status,categoryId);
         }
         else{
             return ServerResponse.createServerResponseByFailure("无权操作");
         }
     }
     //获取商品详情
    @RequestMapping("getProductDetail.do")
    @ResponseBody
    public ServerResponse manageDetail(HttpSession session,Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure("请先登录");
        }
        if (userService.checkAdminRole(user).checkIsSuccess()) {
            return productService.manageDetail(categoryId);
        }
        else{
            return ServerResponse.createServerResponseByFailure("无权操作");
        }
    }
    //分页获取商品列表
    @RequestMapping("getProductList.do")
    @ResponseBody
    public ServerResponse getProductList(HttpSession session, @Param("pageNum") Integer  pageNum,    @Param("pageSize")Integer pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure("请先登录");
        }
        if (userService.checkAdminRole(user).checkIsSuccess()) {
            return productService.selectProductList(pageNum,pageSize);
        }
        else{
            return ServerResponse.createServerResponseByFailure("无权操作");
        }
    }
    //后台搜索商品
    @RequestMapping("searchProduct.do")
    @ResponseBody
    public ServerResponse searchProduct(HttpSession session, @Param("productId") Integer productId, @Param("productName") String productName,
                                                             @Param("pageNum") Integer  pageNum,    @Param("pageSize")Integer pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure("请先登录");
        }
        if (userService.checkAdminRole(user).checkIsSuccess()) {
            return productService.searchProduct(productId,productName,pageNum,pageSize);
        }
        else{
            return ServerResponse.createServerResponseByFailure("无权操作");
        }
    }



     //上传文件
    @RequestMapping("loadProductImage.do")
    @ResponseBody
     public ServerResponse uploadFile(MultipartFile file, HttpServletRequest request){
         String path = request.getSession().getServletContext().getRealPath("upload");//得到绝对路
         String uploadFileName = productService.uploadFile(file,path);
         String url= PropertiesUtil.getProperty("ftp.server.http.prefix")+uploadFileName;
         Map map = Maps.newHashMap();
         map.put("uri",uploadFileName);
         map.put("url",url);
         return ServerResponse.createServerResponseBySuccess(map);

     }
     //富文本上传文件
    @RequestMapping("rich_textLoadProductImage.do")
    @ResponseBody
    public Map richTextUploadFile(MultipartFile file, HttpServletRequest request,HttpSession  session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        Map  resultMap =  Maps.newHashMap();
        if(user==null){
            resultMap.put("success", false);
            resultMap.put("msg", "请登陆管理员");
            return resultMap;
        }
        if (userService.checkAdminRole(user).checkIsSuccess()) {
            String path = request.getSession().getServletContext().getRealPath("upload");//得到绝对路
            String uploadFileName = productService.uploadFile(file, path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + uploadFileName;
            resultMap.put("success", true);
            resultMap.put("msg", "上传成功");
            resultMap.put("file_path", url);
            return resultMap;
        }
        else{
              resultMap.put("success", true);
              resultMap.put("msg", "权限不足");
            return  resultMap;

        }
    }




}
